package org.wx.api.core.handler.impl;

import java.util.Map;

import org.wx.api.core.annotation.ReqType;
import org.wx.api.core.exception.WexinReqException;
import org.wx.api.core.handler.WeiXinReqHandler;
import org.wx.api.core.req.model.WeixinReqConfig;
import org.wx.api.core.req.model.WeixinReqParam;
import org.wx.api.core.util.WeiXinReqUtil;

/**
 * 动态组织生成登录URL地址
 * @author XINJF
 *
 */
public class WeixinReqLoginHandler implements WeiXinReqHandler{

	@Override
	public String doRequest(WeixinReqParam weixinReqParam) throws WexinReqException {
		String returnUrl = null;
		if(weixinReqParam.getClass().isAnnotationPresent(ReqType.class)){
			ReqType reqType = weixinReqParam.getClass().getAnnotation(ReqType.class);
			WeixinReqConfig objConfig = WeiXinReqUtil.getWeixinReqConfig(reqType.value());
			if(objConfig != null){
				String reqUrl = objConfig.getUrl();
				Map<String,Object> parameters = WeiXinReqUtil.getWeixinReqParam(weixinReqParam);
				returnUrl = reqUrl.replace("APPID", (String)parameters.get("appid"))
								  .replace("REDIRECT_URI", (String)parameters.get("redirect_uri"))
								  .replace("SCOPE", (String)parameters.get("scope"))
								  .replace("STATE", (String)parameters.get("state"));
			}
		}
		return returnUrl;
	}

}
